package bio;

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class ClientHandler implements Runnable {

    private Socket socket;
    private RequestHandler requestHandler;

    public ClientHandler(Socket socket, RequestHandler requestHandler) {
        this.socket = socket;
        this.requestHandler = requestHandler;
    }

    @Override
    public void run() {
        try {
            Scanner scanner = new Scanner(socket.getInputStream());
            while (true) {
                String line = scanner.nextLine();
                System.out.println(line);
                String response = (String) requestHandler.handle(line);
                socket.getOutputStream().write(response.getBytes());
                socket.getOutputStream().write("\n".getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
